(*
 *******************************************************
	Package: ODESolve
	Author:  Janos Karsai
	Address: Dept. Medical Informatics,
		 University of Szeged
	         H-6720, Szeged, Hungary
	E-mail:  karsai@dmi.u-szeged.hu
 *******************************************************
*)

(*
Package for solution and visualization of differential equations
version: Mathematica 3.0
*)


(*
*******************************************************
Developed for the book -CDRom: 

	Models of Impulsive systems

	Typotex, Budapest, 2002

* Development is supported by 
* Foundation for the Hungarian Higher Education Research, Grant No. 776/94
* Foundation for the Development of Higher Education Grant No. FEFA-IV 1608/4.
* Hungarian Science Research Foundation OTKA T-034275 
* Hungarian Ministry of Education: Project o. 179/1996
* Pro Renovanda Cultura Hungarie: "Science in Education"
* Hungarian Ministry of Education: Higher Education Textbook Grant, 2001
*
*******************************************************
*)


(* COPYRIGHT_BEGIN
*  Copyright (c) 2002 János Karsai,
*  Department of Medical Informatics, University of Szeged
*
*    
*  Permission to use, copy, modify, distribute, and sell this software and its documentation
*  for any purpose is hereby granted without fee, provided that (i) the above copyright 
*  notices and this permission notice appear in all copies of the software and related
*  documentation, and (ii) the name J. Karsai may not be used in 
*  any advertising or publicity relating to the software without the specific, prior written 
*  permission of J. Karsai.
*    
*  THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY
*  KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT 
*  LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR
*  A PARTICULAR PURPOSE.
*    
*  IN NO EVENT SHALL J. KARSAI BE LIABLE FOR ANY SPECIAL,
*  INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
*  OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
*  OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF 
*  DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 
*  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* COPYRIGHT_END 
*)



BeginPackage["`ODESolve`"]; 

 
ODESolve0::usage =  
"ODESolve0[{rhs1,rhs2,...},{y1,y2,...},{y10,y20,...},{t,t0,T},opt]
solves the ordinary differential system 
on the interval [t0,T]. NDSolve with opt options is used. The right hand
side of the system is {rhs1,rhs2,...}, the dependent 
variables are {y1,y2,...}, The function gives the solution
in the list of InterpolationFunctions with argument.";

ODESolve::usage = 
"ODESolve[{rhs1,rhs2,...},{y1,y2,...},ICTabl,{t,t0,T},opt] solves
the ordinary differential system with multiple set of initial conditions.
on the interval [t0,T]. NDSolve with opt options is used. The right hand
side of the system is {rhs1,rhs2,...}, the dependent 
variables are {y1,y2,...}, ICTabl is a list of initial conditions of form
{{y1-0,y2-0,...}},...,{y1-K,y20-K,...}}
The function gives the solution in the list of InterpolationFunctions with 
argument t.";

ProjectionTraj::usage =
"ProjectionTraj[array,indexlist]. selects the elements of indexes 
given in indexlist from every array[[i]]."

ODEICGen::usage = "Gives the equation with initial condition for ODESolve. ";

ODEGen::usage ="Gives the ODE from its right had side";

ICGen::usage ="Gives the intial contions of form x[t0]==x0...";

FGen::usage ="Gives the form x[t] from x";


Begin["`Private`"];


ODEGen[vec_List, var_List,t_] := 
  MapThread[
    Equal, {var /. Map[ (# -> #'[t]) &, var], 
      vec /. (Map[ (# -> #[t]) &, var])}];

ICGen[IC_, t0_, var_] := 
  Table[var[[i]][t0] == IC[[i]], {i, 1, Length[var]}];

FGen[var_,t_] := var /. (Map[ (# -> #[t]) &, var]);

ODEICGen[vec_List,IC_List, {t0_,t_},var_List]:=Join[ODEGen[vec, var,t],ICGen[IC, t0, var]];


(*
  eqn. solution (InterpolationFunction object)
*)


ODESolve0[xdot_List,xvar_List,icond_List,
		{t_,t0_,T_},opt___]:=
Module[{},
Return[(xvar /. Map[#->#[t]&,#]& [xvar]) /.
First[NDSolve[
	Join[
	Table[
	 xvar[[i]]'[t]==
         (xdot[[i]]/. Map[#->#[t]&,#]& [xvar]),{i,1,Length[xvar]}],
	 Table[xvar[[i]][t0]==icond[[i]],{i,1,Length[xvar]}]],xvar,
	{t,t0,T},opt]
    ]
]];

(*
Solve with a table of init. conditions (table of Interpol.Funct. object)
*)

ODESolve[xdot_List,xvar_List,ictab_List,{t_,t0_,T_},opt___]:=
	Map[(ODESolve0[xdot,xvar,#,{t,t0,T},opt])&,ictab];

(* Create projection of the solutions with the given variables *)

ProjectionTraj[Traj_,Proj_List]:=
Map[Table[#[[Proj[[i]]]],{i,1,Length[Proj]}]&,Traj];
	
End[];

EndPackage[]; 

